package com.santu.edu.supporter.biz.user.service.impl;

import com.santu.edu.supporter.biz.user.dao.UserAdressDao;
import com.santu.edu.supporter.biz.user.pojo.dto.UserAddressDTO;
import com.santu.edu.supporter.biz.user.pojo.params.UserAddressAddParam;
import com.santu.edu.supporter.biz.user.pojo.params.UserAddressUpdateParam;
import com.santu.edu.supporter.biz.user.service.UserAdressService;
import com.santu.edu.supporter.exception.BizException;
import com.santu.edu.supporter.exception.CustomError;
import com.santu.edu.supporter.interceptor.UserThreadLocal;
import com.santu.edu.supporter.util.UuidUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author 12158
 */
@Service
@RequiredArgsConstructor
public class UserAddressServiceImpl implements UserAdressService {

    private final UserAdressDao userAdressDao;

    @Override
    public Boolean addUserAddress(UserAddressAddParam addParam) {
        String addressId = UuidUtils.getUuid(8);
        UserAddressDTO dto = UserAddressDTO.createItem(UserThreadLocal.get(),addressId, addParam);
        userAdressDao.addUserAddress(dto);
        return true;
    }

    @Override
    public Boolean updateUserAddress(UserAddressUpdateParam updateParam) {
        UserAddressDTO dto = UserAddressDTO.createItem(UserThreadLocal.get(),updateParam);
        userAdressDao.updateUserAddress(dto);
        return true;
    }

    @Override
    public Boolean removeUserAddress(String addressId) {
        checkAddressIsExist(addressId);
        userAdressDao.removeUserAddress(addressId);
        return true;
    }

    @Override
    public List<UserAddressDTO> getUserAddressList() {
        return userAdressDao.getUserAddressList(UserThreadLocal.get());
    }

    @Override
    public List<UserAddressDTO> getAddressByIdList(List<String> addressIdList) {
        return userAdressDao.getAddressByIdList(addressIdList);
    }

    public void checkAddressIsExist(String addressId) {
        UserAddressDTO dto = userAdressDao.getAddressById(addressId);
        if (dto == null) {
            throw BizException.causeBy(CustomError.REQUEST_ERROR,"地址不存在");
        }
    }

}
